﻿namespace KingerRobot.MexicoSailunMDC.Biz;
/// <summary>
/// 入库扫描已虚拟点位任务创建
/// </summary>
[Obsolete]
public class LineScannerVirtualCreateBiz : K8.MDCBB.Biz
{
    private const string ClassName = nameof(LineScannerCreateBiz);


    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        /// 扫描仪设备ID
        /// </summary>
        public long LineScannerId { get; set; }
        /// <summary>
        /// 扫描仪位置ID
        /// </summary>
        public long LineScannerLoc { get; set; }
        /// <summary>
        /// 线体编号
        /// </summary>
        public string StationNo { get; set; }
        /// <summary>
        /// 扫描仪名称
        /// </summary>
        public string LineScannerName { get; set; }
    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        /// <summary>
        /// 产品条码
        /// </summary>
        public string ProductCode { get; set; }
        /// <summary>
        /// 计数
        /// </summary>
        public long TireCount { get; set; }
        /// <summary>
        /// 读取任务号（上段线体任务读取后【有值】）才下传新任务
        /// </summary>
        public int RWorkNo { get; set; }
    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {
        /// <summary>
        /// 计数
        /// </summary>
        public long TireCount { get; set; }
    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{ClassName}|{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion 

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizData
        this.BizData = writer;
        // 返回参数
        var result = 0;

        //// 获取条码
        //var productCode = Value.ProductCode;
        //// 条码为空
        //if (string.IsNullOrWhiteSpace(productCode) || productCode.StartsWith(ConstFields.Default))
        //{
        //    // 已扫描条码
        //    this.StepMsg($"{productCode}:条码为空，线体流水号{Value.RWorkNo}，扫描仪{Param.LineScannerId}-{Param.LineScannerLoc}-{Param.LineScannerName}");
        //    return result;
        //}
        //if (productCode.StartsWith(ConstFields.NoRead))
        //{
        //    //productCode = ConstFields.NoRead;
        //}
        //this.StepMsg($"LineScannerVirtualCreate记录条码{productCode},缓存计数{Cache.TireCount},扫描计数{Value.TireCount}");


        //// 已扫描创建任务
        //if (Cache.TireCount == this.Value.TireCount)
        //{
        //    this.StepMsg($"第{Value.TireCount}条码{productCode}任务已创建");
        //    return result;
        //}

        //// 获取条码是否已经创建
        //var barCodeService = K8.RSF.Client.Create<IScannerService>();
        //var isScanner = barCodeService.IsLogScannerByProductCode(productCode, Value.TireCount);
        //if (isScanner)
        //{
        //    Cache.TireCount = this.Value.TireCount;
        //    this.StepMsg($"{Value.TireCount}通过日志记录跟踪，条码 {productCode}任务已创建");
        //    return result;
        //}

        //// 创建任务
        //var num = barCodeService.AddLogScanner(Param.LineScannerLoc, productCode, Value.TireCount);

        //this.StepMsg($"{productCode},扫描记录创建标识:{num}, " +
        //    $"扫描仪{Param.LineScannerId}-{Param.LineScannerLoc}-{Param.LineScannerName} | 线体编号:{Param.StationNo},线体任务:{Value.RWorkNo}");

        //// 创建扫码记录
        //var service = K8.RSF.Client.Create<IWarehousingBeforeService>();
        //var taskId = service.CreateTask(Param.LineScannerLoc, productCode, 0, Value.RWorkNo, Param.StationNo);

        //var taskResult = taskId > 0 ? "成功" : "失败";
        //this.StepMsg($"{taskResult}，扫描仪{Param.LineScannerId}-{Param.LineScannerLoc}-{Param.LineScannerName} |" +
        // $"创建任务参数(productCode):{productCode} |" +
        // $"返回值:{taskId}");

        //// 状态
        //var status = (int)EnumScannerStatus.Success;
        //if (taskId <= 0)
        //{
        //    this.StepMsg($"{productCode}，任务创建失败：编号{taskId}【编号：-8,-9直线口缓存满】");
        //    status = (int)EnumScannerStatus.Fail;
        //}
        //else
        //{
        //    Cache.TireCount = Value.TireCount;
        //}

        //// 修改扫码完成记录
        //barCodeService.UpdateLogScannerByProductCode(productCode,
        //    Value.TireCount,
        //    status, (status == (int)EnumScannerStatus.Success ? $"创建成功{taskId}" : $"创建失败,编号:{taskId}"));

        return result;
    }
}
